package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	response "hepan1/src/common/answer"
	"hepan1/src/model"
	"hepan1/src/setting"
	"time"
)

func HaveLogin()gin.HandlerFunc{
	return func(context *gin.Context) {
		uid, _ := context.Cookie("choujiang_name")
		var user model.Drawusers
		affect := model.Db.Where("openid = ?",uid).First(&user).RowsAffected
		//stm := fmt.Sprintf("select openid from drawusers where openid = '%s'",uid)
		//fmt.Println(stm)
		//var openid string
		//valid := model.Db.QueryRow(stm).Scan(&openid)
		if affect <= 0{
			response.Fail(context,1,"无法获取cookies，请重新登录")
			context.Abort()
		}else {
			context.Next()
		}

	}
}

// Ragnarok 查看目前时间是否已经到截止时间
func Ragnarok()gin.HandlerFunc{
	return func(context *gin.Context) {
		t := time.Now().Unix()
		fmt.Println(t)
		if t >= setting.Endoftime {
			model.PushTheLucky()
		}
	}
}